<template>
  <div class="app-container">
    <h2 style="text-align: center">硬笔书详细添加</h2>

    <el-steps
      :active="2"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="添加单元课时" />
      <el-step title="添加课时内容" />
      <el-step title="添加生字学习信息" />
    </el-steps>


    <div class="block">
      <el-button type="primary" @click="addChapterPic();">添加课时图片</el-button>
      <el-dialog title="添加课时图片" :visible.sync="dialogFormVisible" :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="120px" class="demo-ruleForm">
          <el-form-item label="硬笔系统封面：" prop="chapterPicture" :rules="[{ required: true, message: '硬笔系统封面未上传'}]">
            <el-upload 
            class="avatar-uploader" 
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :on-success="handleAvatarSuccess"
            :auto-upload="true"
            :show-file-list="false">
              <img v-if="form.chapterPicture" :src="form.chapterPicture" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="课时顺序："
              prop="corder"
              :rules="[{ required: true, message: '课时顺序不能为空'}]">
              <el-input-number v-model="form.corder" controls-position="right" :min="0"></el-input-number>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="insertChapter();">保 存</el-button>
        </div>
      </el-dialog>
      <el-table
        :data="dataList"
        stripe
        v-loading="dataListLoading"
        style="width: 100%"
      >
        <el-table-column prop="id" label="课时图片id" width="180">
        </el-table-column>
        <el-table-column prop="corder" label="课时顺序" width="180">
        </el-table-column>
        <el-table-column prop="chapterPicture" label="课时图片">
            <template slot-scope="scope">
                <el-image :src="scope.row.chapterPicture"></el-image>
          </template> </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="updateChapter(scope.row.id,scope.row.chapterPicture,scope.row.corder)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="deleteChapter(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页条 -->
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chapterId: '',
      dataListLoading: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      form: {
        chapterPicture: '',
        corder: '',
        updateId: '',
      },
      dialogFormVisible: false,
    };
  },
  
  activated() {
    this.chapterId = this.$route.params.id;
    this.getChapterPicByCid();
  },

  methods: {
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    handleClose(){
      this.dialogFormVisible = false;
    },
    //删除课时图片
    deleteChapter(id){
      this.$confirm(`确定对[课时图片id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbstiffchapterpicture/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getChapterPicByCid();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    //更新课时图片
    updateChapter(id,chapterPicture,corder){
      this.form.updateId = id;
      this.form.chapterPicture = chapterPicture;
      this.form.corder = corder;
      this.addChapterPic();
    },
    //上传成功后的回调函数
    handleAvatarSuccess(response) {
      this.form.chapterPicture = response.url;
    },
    //添加课时图片
    insertChapter(){
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if(this.form.updateId == ''){
            this.$http({
              url: this.$http.adornUrl("/sf/cbstiffchapterpicture/save"),
              method: "post",
              data: this.$http.adornParams({
                chapterPicture: this.form.chapterPicture,
                corder: this.form.corder,
                cid: this.chapterId,
              }),
            }).then(({ data }) => {
              this.form.chapterPicture = '';
              this.form.corder = '';
              this.dialogFormVisible = false;
              this.$message({
                message: '添加成功!',
                type: 'success'
              });
              this.getChapterPicByCid();
            });
          }else{
            this.$http({
              url: this.$http.adornUrl("/sf/cbstiffchapterpicture/update"),
              method: "post",
              data: this.$http.adornParams({
                id: this.form.updateId,
                chapterPicture: this.form.chapterPicture,
                corder: this.form.corder,
                cid: this.chapterId,
              }),
            }).then(({ data }) => {
              this.form.updateId = '';
              this.form.chapterPicture = '';
              this.form.corder = '';
              this.dialogFormVisible = false;
              this.$message({
                message: '修改成功!',
                type: 'success'
              });
              this.getChapterPicByCid();
            });
          }
        }else{
          this.$message.error('课时图片未上传或课时顺序不能为空！');
        }
      });
    },
    //打开添加课时图片模态框
    addChapterPic(){
      this.dialogFormVisible = true;
    },
    //获取一章节所有图片
    getChapterPicByCid(){
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sf/cbstiffchapterpicture/list"),
        method: "get",
        params: this.$http.adornParams({
          cid: this.chapterId,
          page: this.pageIndex,
          limit: this.pageSize,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.stiffChapterPicturePage.records;
          this.totalPage = data.stiffChapterPicturePage.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
  },
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
